import Vue from 'vue'
import { baseURL } from '../../common/config.js'

const tools = {};
// 接口前缀
tools.baseURL = baseURL
// 跳转
tools.navigate = function(path, redirect = false) {
	if(redirect) {
		uni.switchTab({
			url: path,
		});
		try {
			uni.setStorageSync('jumpTo', 2);
		} catch (e) {
		}
	} else {
		uni.navigateTo({
			url: path,
		});
	}
}
tools.back = function(delta) {
	uni.navigateBack({
		delta: delta
	});
}
// 错误消息提示
tools.toast = function(res) {
	uni.showToast({
		icon: 'none',
		title: res,
		duration: 2000
	});
}
// 成功消息提示
tools.succend = function(res) {
	uni.showToast({
		title: res,
		duration: 2000
	});
}
// 身份切换
tools.change = function(type) {
	if(type==0) {
		uni.setStorage({
			key: 'Type',
			data: 1,
		});
		uni.setTabBarItem({
			index: 0,
			text: '牛人',
		})
		uni.setTabBarItem({
			index: 1,
			text: '发布需求',
		})

		uni.reLaunch({
			url: '/pages/home/index',
		});
	} else if(type==1) {
		uni.setStorage({
			key: 'Type',
			data: 0,
		});
		uni.setTabBarItem({
			index: 0,
			text: '找需求',
		})
		uni.setTabBarItem({
			index: 1,
			text: '发布接单',
		})

		uni.reLaunch({
			url: '/pages/home/index',
		});
	}
}
tools.toOpenNotificationSettings = function() {
 return new Promise((resolve, reject) => {
  var isOn = undefined;
  try {
   if ('iOS' == plus.os.name) {
    var app = plus.ios.invoke('UIApplication', 'sharedApplication');
    var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
    plus.ios.invoke(app, 'openURL:', setting);
    plus.ios.deleteObject(setting);
    plus.ios.deleteObject(app);
   } else {
    var main = plus.android.runtimeMainActivity();
    var Intent = plus.android.importClass('android.content.Intent');
    var Build = plus.android.importClass("android.os.Build");
    var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
    var pkName = main.getPackageName();
    var uid = main.getApplicationInfo().plusGetAttribute("uid");
    //android 8.0引导  
    if (Build.VERSION.SDK_INT >= 26) {
     intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
    } else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0  
     intent.putExtra("app_package", pkName);
     intent.putExtra("app_uid", uid);
    } else { //(<21)其他--跳转到该应用管理的详情页  
     var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
     intent.setData(uri);
    }
    // 跳转到该应用的系统通知设置页  
    main.startActivity(intent);
   }
  } catch (e) {
   reject('exception in isTurnedOnPush!!');
  }
  resolve(isOn);
 })
}
// 时间处理 年月日
tools.dateFormatSm = function(time){
	let date = new Date(time) ;
	let year = date.getFullYear();
	// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
	let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	let day = date.getDate() < 10 ? "0" + date.getDate() :date.getDate()
	// 拼接
	// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
	return year + "-" + month + "-" + day ;
}
// 时间处理 年月日时分秒
tools.dateFormat = function(time){
	let date = new Date(time);
	let year = date.getFullYear();
	// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
	let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
	let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	// 拼接
	// return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
	return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
export default tools;
